import {
    Component,
    ElementRef,
    EventEmitter,
    Injector,
    Output,
    ViewChild,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
    PlacesServiceProxy,
    UserPlaceListDto,
    ParkingsBillingSchemeServiceProxy,
    ParkingsBillingSchemeListDto,
    ComboboxItemDto,
    PlaceEditDto,
    GetPlaceForEditOutput,
    UpdatePlace,
    PaymethodServiceProxy,
    CreatePaymethodDto,
} from "@shared/service-proxies/service-proxies";
import { id } from "@swimlane/ngx-charts/release/utils";
import { ModalDirective } from "ngx-bootstrap";
import * as moment from "moment";
import { finalize } from "rxjs/operators";
import { Data } from "@angular/router";
import { result } from "lodash";

@Component({
    selector: "createOrEditRenewPlaceModal",
    templateUrl: "./create-or-edit-renew-place-modal.component.html",
})
export class CreateOrEditRenewPlaceModalComponent extends AppComponentBase {
    @ViewChild("createOrEditModal") modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    neighbourhood_id: number;
    neighbourhood_name: string;
    parkingCode: string;
    parkingId: number;
    parkingName: string;
    placeId: number;
    placeName: string;
    placeNo: string;
    placeSatus: number;
    billingScheme = new ParkingsBillingSchemeListDto();
    monthlyRentPrice: number;
    quarterlyPrice: number;
    halfYearPrice: number;
    yearPrice: number;
    SellingPrice: number;
    payTypes: ComboboxItemDto[] = [];
    emptyText = "请选择付款期限";
    payvalue: string;
    newDate: Date;
    etime = new Date();
    newEndTime  = moment(this.etime).format('YYYY-MM-DD');
    userid: number;
    getPlaceForEditOutput: GetPlaceForEditOutput = new GetPlaceForEditOutput();
    PlaceEdit: PlaceEditDto = new PlaceEditDto();
    payType:any;
    constructor(
        injector: Injector,
        private _placesServiceProxy: PlacesServiceProxy,
        private _parkingsBillingSchemeServiceProxy: ParkingsBillingSchemeServiceProxy,
        private _paymethodServiceProxy: PaymethodServiceProxy
    ) {
        super(injector);
    }

    show(UserPlaceList: UserPlaceListDto, userId: number): void {
        const self = this;
        self.active = true;

        (this.neighbourhood_id = UserPlaceList.neighbourhood_id),
            (this.neighbourhood_name = UserPlaceList.neighbourhood_name),
            (this.parkingCode = UserPlaceList.parkingCode),
            (this.parkingId = UserPlaceList.parkingId),
            (this.parkingName = UserPlaceList.parkingName),
            (this.placeId = UserPlaceList.placeId),
            (this.placeName = UserPlaceList.placeName);
        // this.etime = UserPlaceList.endTime.
        this.userid = userId;
        this.etime = UserPlaceList.endTime
            ? UserPlaceList.endTime.year() === 1
                ? undefined
                : UserPlaceList.endTime.toDate()
            : undefined;

        this.newEndTime  = moment(this.etime).format('YYYY-MM-DD');
        this.placeNo = UserPlaceList.placeNo;
        this.placeSatus = UserPlaceList.placeSatus;
        this._parkingsBillingSchemeServiceProxy
            .getAll(this.parkingId,true, "", 0, 200)
            .subscribe((result) => {
                console.log(result.items[0]);
                //  this.billingScheme = result.items[0];
                (this.monthlyRentPrice = result.items[0].monthlyRentPrice),
                    (this.quarterlyPrice = result.items[0].quarterlyPrice),
                    (this.halfYearPrice = result.items[0].halfYearPrice),
                    (this.yearPrice = result.items[0].yearPrice);
                if (this.payTypes.length == 0) {
                    let monthlyRentPrice = new ComboboxItemDto();
                    (monthlyRentPrice.displayText =
                        "月付" + this.monthlyRentPrice + "元/月"),
                        (monthlyRentPrice.value =
                            this.monthlyRentPrice.toString()),
                        this.payTypes.push(monthlyRentPrice);

                    let quarterlyPrice = new ComboboxItemDto();
                    (quarterlyPrice.displayText =
                        "季付" + this.quarterlyPrice + "元/月"),
                        (quarterlyPrice.value = (
                            this.quarterlyPrice * 3
                        ).toString()),
                        this.payTypes.push(quarterlyPrice);
                    let halfYearPrice = new ComboboxItemDto();
                    (halfYearPrice.displayText =
                        "半年付" + this.halfYearPrice + "元/月"),
                        (halfYearPrice.value = (
                            this.halfYearPrice * 6
                        ).toString()),
                        this.payTypes.push(halfYearPrice);

                    let yearPrice = new ComboboxItemDto();
                    yearPrice.displayText = "年付" + this.yearPrice + "元/月";
                    (yearPrice.value = (this.yearPrice * 12).toString()),
                        this.payTypes.push(yearPrice);
                }
                console.log(UserPlaceList);
                console.log(this.payTypes);
                console.log(this.etime);
            });

        self.modal.show();
    }

    onShown(): void {}

    save(): void {
        const self = this;
        let PlaceEdit = new PlaceEditDto();
        let month = 0;
        // if (Number(this.payvalue) == this.monthlyRentPrice) {
        //     month = 1;
        // }
        // if (Number(this.payvalue) == this.quarterlyPrice * 3) {
        //     month = 3;
        // }
        // if (Number(this.payvalue) == this.halfYearPrice * 6) {
        //     month = 6;
        // }
        // if (Number(this.payvalue) == this.yearPrice * 12) {
        //     month = 12;
        // }
        
        // let etime = this.etime
        //     ? moment(this.etime).add(month, "months")
        //     : undefined;

        // self._placesServiceProxy
        //     .getPlaceForEdit(this.placeId)
        //     .subscribe((result) => {
        //         PlaceEdit = result.placeEdit;
        //         PlaceEdit.endTime = etime;

        //         let input = new UpdatePlace();
        //         input = PlaceEdit;
        //         console.log(PlaceEdit);
        //         this._placesServiceProxy
        //             .update(input)
        //             .pipe(finalize(() => (this.saving = false)))
        //             .subscribe(() => {
        //                 this.notify.info(this.l("SavedSuccessfully"));
        //                 this.close();
        //                 this.modalSave.emit(null);
        //             });
        //     });

        // let CreatePayment = new CreatePaymethodDto();
        // (CreatePayment.cost = Number(this.payvalue)),
        //     (CreatePayment.cycle = month);
        // (CreatePayment.neighbourhood_id = this.neighbourhood_id),
        //     (CreatePayment.parkingId = this.parkingId),
        //     (CreatePayment.placeId = this.placeId),
        //     (CreatePayment.userId = this.userid);
        // this._paymethodServiceProxy
        //     .create(CreatePayment)
        //     .subscribe((result) => {});

        let payTypeList = this.payTypes.filter(e=>e.displayText == this.payType);
        if(payTypeList.length == 0){
           this.notify.error("请选择付款类型");
            return ;
        }
        switch(payTypeList[0].displayText.toString().substring(0,1)){
            case "月":
                month = 1;
                break;
            case "季":
                month = 3;
                break;
            case "半":
                month = 6;
                break;
            case "年":
                month = 12;
                break;
            default:
                break
        }

        this._placesServiceProxy
            .updateEndTime(this.placeId, this.userid,month, Number(this.payvalue))
            .subscribe(() => {
                this.notify.info(this.l("SavedSuccessfully"));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
    getParkingsBillingScheme() {}

    changePayType() {
        let payTypeStart = this.payType.toString().substring(0,1);
        let payTypeList = this.payTypes.filter(e=>e.displayText == this.payType);
        if(payTypeList.length == 0){
            this.newEndTime = moment(this.etime).format('YYYY-MM-DD');
            this.payvalue = "";
            return ;
        }

        this.payvalue = payTypeList[0].value;
        let month = 0;
        switch(payTypeStart){
            case "月":
                month = 1;
                break;
            case "季":
                month = 3;
                break;
            case "半":
                month = 6;
                break;
            case "年":
                month = 12;
                break;
            default:
               
                this.payvalue = "";
                return;
        }
        
        this.newEndTime = moment(this.etime).add(month,"M").format('YYYY-MM-DD');
    }
}
